/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package component.xml;

import Globals.AllGlobal;
import database.operate.OperateMyDatabaseImpl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.GongZuoMian_Model;
import model.ZuanChang_Model;
import model.ZuanKong_Model;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author Administrator
 */
public class XMLSave {
    private String filepath="";
    private final Element root = DocumentHelper.createElement("采场");
    private final Document document = DocumentHelper.createDocument(root);
    public XMLSave(String filepath){
        this.filepath=filepath;
        Element root = DocumentHelper.createElement("采场");
        Document document = DocumentHelper.createDocument(root);
    }
    public void saveData(){
        try {
            OutputFormat format = new OutputFormat("    ", true);// 设置缩进为4个空格，并且另起一行为true
            XMLWriter xmlWriter = new XMLWriter(
                    new FileOutputStream(filepath), format);
            xmlWriter.write(document);
        } catch (Exception ex) {
            Logger.getLogger(XMLSave.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void deleteData(ZuanChang_Model zuanChang_Model){
        Iterator<Element> gzmit=root.elementIterator("工作面");
        while(gzmit.hasNext()){
            Element gzmElement=gzmit.next();
            List<Attribute> attrList=gzmElement.attributes();
            if(attrList.get(0).equals(zuanChang_Model.getGongzuomian())){
                Iterator<Element> zcIt=gzmElement.elementIterator("钻场");
                while(zcIt.hasNext()){
                    Element zcElement=zcIt.next();
                    List<Attribute> zcattrList=zcElement.attributes();
                    if(zcattrList.get(0).equals(zuanChang_Model.getMingcheng())){
                        gzmElement.remove(zcElement);                        
                    }
                }                              
            }
        }
    }
    public void deleteData(ZuanKong_Model zuanKong_Model) throws Exception{
        Iterator<Element> gzmit=root.elementIterator("工作面");
        while(gzmit.hasNext()){
            Element gzmElement=gzmit.next();
            List<Attribute> attrList=gzmElement.attributes();
            ArrayList<ZuanChang_Model> zcOne= OperateMyDatabaseImpl.getOne(AllGlobal.ZUANCHANG_SQL, 
                    "select * from zuanchang where mingcheng='"+zuanKong_Model.getZuanchang()+"';");
            if(zcOne.size()>0){
                if(attrList.get(0).equals(zcOne.get(0).getGongzuomian())){
                    Iterator<Element> zcIt=gzmElement.elementIterator("钻场");
                    while(zcIt.hasNext()){
                        Element zcElement=zcIt.next();
                        List<Attribute> zcatttList=zcElement.attributes();
                        if(zcatttList.get(0).equals(zuanKong_Model.getZuanchang())){
                            Iterator<Element> zkIt=zcElement.elementIterator("钻孔");
                            while(zkIt.hasNext()){
                                Element zkElement=zkIt.next();
                                List<Attribute> zkattrList=zkElement.attributes();
                                if(zkattrList.get(0).equals(zuanKong_Model.getMingcheng())){
                                    zcElement.remove(zkElement);                                   
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    public void deleteData(GongZuoMian_Model gongZuoMian_Model){
        Iterator<Element> it=root.elementIterator("工作面");
        while(it.hasNext()){
            Element gzmElement=it.next();
            List<Attribute> attrList=gzmElement.attributes();
            if(attrList.get(0).equals(gongZuoMian_Model.getMingcheng())){
                root.remove(gzmElement);
            }
        }
    }
    public void changeData(ZuanChang_Model zuanChang_Model){
        Iterator<Element> gzmit=root.elementIterator("工作面");
        while(gzmit.hasNext()){
            Element gzmElement=gzmit.next();
            List<Attribute> attrList=gzmElement.attributes();
            if(attrList.get(0).equals(zuanChang_Model.getGongzuomian())){
                Iterator<Element> zcIt=gzmElement.elementIterator("钻场");
                while(zcIt.hasNext()){
                    Element zcElement=zcIt.next();
                    List<Attribute> zcattrList=zcElement.attributes();
                    if(zcattrList.get(0).equals(zuanChang_Model.getMingcheng())){
                        gzmElement.remove(zcElement);
                        Element zcNewElement=gzmElement.addElement("钻场");
                        zcNewElement.addAttribute("mingcheng", zuanChang_Model.getMingcheng());
                        zcNewElement.addAttribute("shendu", zuanChang_Model.getShendu());
                        zcNewElement.addAttribute("xdzbx", zuanChang_Model.getXdzbx());
                        zcNewElement.addAttribute("xdzby", zuanChang_Model.getXdzby());
                        zcNewElement.addAttribute("gongzuomian", zuanChang_Model.getGongzuomian());
                        zcNewElement.addAttribute("yanzhanxing", zuanChang_Model.getYanzhanxing());
                        zcNewElement.addAttribute("kongxilv", zuanChang_Model.getKongxilvString());
                    }
                }                              
            }
        }
    }
    public void changeData(ZuanKong_Model zuanKong_Model) throws Exception{
        Iterator<Element> gzmit=root.elementIterator("工作面");
        while(gzmit.hasNext()){
            Element gzmElement=gzmit.next();
            List<Attribute> attrList=gzmElement.attributes();
            ArrayList<ZuanChang_Model> zcOne= OperateMyDatabaseImpl.getOne(AllGlobal.ZUANCHANG_SQL, 
                    "select * from zuanchang where mingcheng='"+zuanKong_Model.getZuanchang()+"';");
            if(zcOne.size()>0){
                if(attrList.get(0).equals(zcOne.get(0).getGongzuomian())){
                    Iterator<Element> zcIt=gzmElement.elementIterator("钻场");
                    while(zcIt.hasNext()){
                        Element zcElement=zcIt.next();
                        List<Attribute> zcatttList=zcElement.attributes();
                        if(zcatttList.get(0).equals(zuanKong_Model.getZuanchang())){
                            Iterator<Element> zkIt=zcElement.elementIterator("钻孔");
                            while(zkIt.hasNext()){
                                Element zkElement=zkIt.next();
                                List<Attribute> zkattrList=zkElement.attributes();
                                if(zkattrList.get(0).equals(zuanKong_Model.getMingcheng())){
                                    zcElement.remove(zkElement);
                                    Element zkNewElement=zcElement.addElement("钻孔");
                                    zkNewElement.addAttribute("mingcheng", zuanKong_Model.getMingcheng());
                                    zkNewElement.addAttribute("fangweijiao", zuanKong_Model.getFangwei());
                                    zkNewElement.addAttribute("qingjiao", zuanKong_Model.getQingjiao());
                                    zkNewElement.addAttribute("chengkong", zuanKong_Model.getChengkong());
                                    zkNewElement.addAttribute("zhujiangqian", zuanKong_Model.getZhujiangqian());
                                    zkNewElement.addAttribute("shuiya", zuanKong_Model.getShuiya());
                                    zkNewElement.addAttribute("kongshen", zuanKong_Model.getKongshen());
                                    zkNewElement.addAttribute("va", zuanKong_Model.getVa());
                                    zkNewElement.addAttribute("vb", zuanKong_Model.getVb());
                                    zkNewElement.addAttribute("vc", zuanKong_Model.getVc());
                                    zkNewElement.addAttribute("niantu", zuanKong_Model.getNiantu());
                                    zkNewElement.addAttribute("shuini", zuanKong_Model.getShuini());
                                    zkNewElement.addAttribute("zhongya", zuanKong_Model.getZhongya());
                                    zkNewElement.addAttribute("beizhu", zuanKong_Model.getBeizhu());
                                    zkNewElement.addAttribute("zuanchang", zuanKong_Model.getZuanchang());
                                    zkNewElement.addAttribute("csdjl", zuanKong_Model.getChushuidianshuipingtouying());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    public void changeData(GongZuoMian_Model gongZuoMian_Model){
        Iterator<Element> it=root.elementIterator("工作面");
        while(it.hasNext()){
            Element gzmElement=it.next();
            List<Attribute> attrList=gzmElement.attributes();
            if(attrList.get(0).equals(gongZuoMian_Model.getMingcheng())){
                root.remove(gzmElement);
                Element gzmNewElement=root.addElement("工作面");
                gzmNewElement.addAttribute("mingcheng", gongZuoMian_Model.getMingcheng());
                gzmNewElement.addAttribute("zhitu", gongZuoMian_Model.getZhitu());
                gzmNewElement.addAttribute("制图日期", gongZuoMian_Model.getZhituriqi());
                gzmNewElement.addAttribute("hedui", gongZuoMian_Model.getHedui());
                gzmNewElement.addAttribute("bilichi", gongZuoMian_Model.getBilichiString());
                gzmNewElement.addAttribute("kezhang", gongZuoMian_Model.getKezhang());
                gzmNewElement.addAttribute("bianhao", gongZuoMian_Model.getBianhao());
                gzmNewElement.addAttribute("gongchengshi", gongZuoMian_Model.getGongchengshi());
                gzmNewElement.addAttribute("caichang", gongZuoMian_Model.getCaichang());
                gzmNewElement.addAttribute("gzmcd", gongZuoMian_Model.getGzmcd());
                gzmNewElement.addAttribute("gzmzxcd", gongZuoMian_Model.getGzmzxcdString());
                gzmNewElement.addAttribute("gzmgszbx", gongZuoMian_Model.getGzmgszbX());
                gzmNewElement.addAttribute("gzmgszby", gongZuoMian_Model.getGzmgszbY());
                gzmNewElement.addAttribute("gzmfwj", gongZuoMian_Model.getGzmfwj());
                gzmNewElement.addAttribute("ahjl", gongZuoMian_Model.getAhcjl());
                gzmNewElement.addAttribute("sgqsgszbx", gongZuoMian_Model.getSgqsgszbx());
                gzmNewElement.addAttribute("sgqsgszby", gongZuoMian_Model.getSgqsgszby());
            }
        }
        
    }
    public void addData(ZuanChang_Model zuanChang_Model){
        Iterator<Element> gzmit=root.elementIterator("工作面");
        while(gzmit.hasNext()){
            Element gzmElement=gzmit.next();
            List<Attribute> attrList=gzmElement.attributes();
            if(attrList.get(0).equals(zuanChang_Model.getGongzuomian())){
                Element zcElement=gzmElement.addElement("钻场");
                zcElement.addAttribute("mingcheng", zuanChang_Model.getMingcheng());
                zcElement.addAttribute("shendu", zuanChang_Model.getShendu());
                zcElement.addAttribute("xdzbx", zuanChang_Model.getXdzbx());
                zcElement.addAttribute("xdzby", zuanChang_Model.getXdzby());
                zcElement.addAttribute("gongzuomian", zuanChang_Model.getGongzuomian());
                zcElement.addAttribute("yanzhanxing", zuanChang_Model.getYanzhanxing());
                zcElement.addAttribute("kongxilv", zuanChang_Model.getKongxilvString());
            }
        }
    }
    public void addData(ZuanKong_Model zuanKong_Model)throws Exception{
        Iterator<Element> gzmit=root.elementIterator("工作面");
        while(gzmit.hasNext()){
            Element gzmElement=gzmit.next();
            List<Attribute> attrList=gzmElement.attributes();
            ArrayList<ZuanChang_Model> zcOne= OperateMyDatabaseImpl.getOne(AllGlobal.ZUANCHANG_SQL, 
                    "select * from zuanchang where mingcheng='"+zuanKong_Model.getZuanchang()+"';");
            if(zcOne.size()>0){
                if(attrList.get(0).equals(zcOne.get(0).getGongzuomian())){
                    Iterator<Element> zcIt=gzmElement.elementIterator("钻场");
                    while(zcIt.hasNext()){
                        Element zcElement=zcIt.next();
                        List<Attribute> zcatttList=zcElement.attributes();
                        if(zcatttList.get(0).equals(zuanKong_Model.getZuanchang())){
                            Element zkElement=zcElement.addElement("钻孔");
                            zkElement.addAttribute("mingcheng", zuanKong_Model.getMingcheng());
                            zkElement.addAttribute("fangweijiao", zuanKong_Model.getFangwei());
                            zkElement.addAttribute("qingjiao", zuanKong_Model.getQingjiao());
                            zkElement.addAttribute("chengkong", zuanKong_Model.getChengkong());
                            zkElement.addAttribute("zhujiangqian", zuanKong_Model.getZhujiangqian());
                            zkElement.addAttribute("shuiya", zuanKong_Model.getShuiya());
                            zkElement.addAttribute("kongshen", zuanKong_Model.getKongshen());
                            zkElement.addAttribute("va", zuanKong_Model.getVa());
                            zkElement.addAttribute("vb", zuanKong_Model.getVb());
                            zkElement.addAttribute("vc", zuanKong_Model.getVc());
                            zkElement.addAttribute("niantu", zuanKong_Model.getNiantu());
                            zkElement.addAttribute("shuini", zuanKong_Model.getShuini());
                            zkElement.addAttribute("zhongya", zuanKong_Model.getZhongya());
                            zkElement.addAttribute("beizhu", zuanKong_Model.getBeizhu());
                            zkElement.addAttribute("zuanchang", zuanKong_Model.getZuanchang());
                            zkElement.addAttribute("csdjl", zuanKong_Model.getChushuidianshuipingtouying());
                        }
                    }
                }
            }
        }
    }
    public void addData(GongZuoMian_Model gongZuoMian_Model){
        Element gzmElement=root.addElement("工作面");
        gzmElement.addAttribute("mingcheng", gongZuoMian_Model.getMingcheng());
        gzmElement.addAttribute("zhitu", gongZuoMian_Model.getZhitu());
        gzmElement.addAttribute("制图日期", gongZuoMian_Model.getZhituriqi());
        gzmElement.addAttribute("hedui", gongZuoMian_Model.getHedui());
        gzmElement.addAttribute("bilichi", gongZuoMian_Model.getBilichiString());
        gzmElement.addAttribute("kezhang", gongZuoMian_Model.getKezhang());
        gzmElement.addAttribute("bianhao", gongZuoMian_Model.getBianhao());
        gzmElement.addAttribute("gongchengshi", gongZuoMian_Model.getGongchengshi());
        gzmElement.addAttribute("caichang", gongZuoMian_Model.getCaichang());
        gzmElement.addAttribute("gzmcd", gongZuoMian_Model.getGzmcd());
        gzmElement.addAttribute("gzmzxcd", gongZuoMian_Model.getGzmzxcdString());
        gzmElement.addAttribute("gzmgszbx", gongZuoMian_Model.getGzmgszbX());
        gzmElement.addAttribute("gzmgszby", gongZuoMian_Model.getGzmgszbY());
        gzmElement.addAttribute("gzmfwj", gongZuoMian_Model.getGzmfwj());
        gzmElement.addAttribute("ahjl", gongZuoMian_Model.getAhcjl());
        gzmElement.addAttribute("sgqsgszbx", gongZuoMian_Model.getSgqsgszbx());
        gzmElement.addAttribute("sgqsgszby", gongZuoMian_Model.getSgqsgszby());
    }
    public void addAll() throws Exception{
        ArrayList<GongZuoMian_Model> gzmList=OperateMyDatabaseImpl.getAllResult(AllGlobal.GONGZUOMIAN_SQL);
        ArrayList<ZuanChang_Model> zcList=OperateMyDatabaseImpl.getAllResult(AllGlobal.ZUANCHANG_SQL);
        ArrayList<ZuanKong_Model> zkList=OperateMyDatabaseImpl.getAllResult(AllGlobal.ZUANKONG_SQL);
        for(int i=0;i<gzmList.size();i++){
            Element gzmElement=root.addElement("工作面");
            gzmElement.addAttribute("mingcheng", gzmList.get(i).getMingcheng());
            gzmElement.addAttribute("zhitu", gzmList.get(i).getZhitu());
            gzmElement.addAttribute("制图日期", gzmList.get(i).getZhituriqi());
            gzmElement.addAttribute("hedui", gzmList.get(i).getHedui());
            gzmElement.addAttribute("bilichi", gzmList.get(i).getBilichiString());
            gzmElement.addAttribute("kezhang", gzmList.get(i).getKezhang());
            gzmElement.addAttribute("bianhao", gzmList.get(i).getBianhao());
            gzmElement.addAttribute("gongchengshi", gzmList.get(i).getGongchengshi());
            gzmElement.addAttribute("caichang", gzmList.get(i).getCaichang());
            gzmElement.addAttribute("gzmcd", gzmList.get(i).getGzmcd());
            gzmElement.addAttribute("gzmzxcd", gzmList.get(i).getGzmzxcdString());
            gzmElement.addAttribute("gzmgszbx", gzmList.get(i).getGzmgszbX());
            gzmElement.addAttribute("gzmgszby", gzmList.get(i).getGzmgszbY());
            gzmElement.addAttribute("gzmfwj", gzmList.get(i).getGzmfwj());
            gzmElement.addAttribute("ahjl", gzmList.get(i).getAhcjl());
            gzmElement.addAttribute("sgqsgszbx", gzmList.get(i).getSgqsgszbx());
            gzmElement.addAttribute("sgqsgszby", gzmList.get(i).getSgqsgszby());
            for(int j=0;j<zcList.size();j++){
                if(zcList.get(j).getGongzuomian().equals(gzmList.get(i).getMingcheng())){
                    Element zcElement=gzmElement.addElement("钻场");
                    zcElement.addAttribute("mingcheng", zcList.get(j).getMingcheng());
                    zcElement.addAttribute("shendu", zcList.get(j).getShendu());
                    zcElement.addAttribute("xdzbx", zcList.get(j).getXdzbx());
                    zcElement.addAttribute("xdzby", zcList.get(j).getXdzby());
                    zcElement.addAttribute("gongzuomian", zcList.get(j).getGongzuomian());
                    zcElement.addAttribute("yanzhanxing", zcList.get(j).getYanzhanxing());
                    zcElement.addAttribute("kongxilv", zcList.get(j).getKongxilvString());
                    for(int k=0;k<zkList.size();k++){
                        if(zkList.get(k).getZuanchang().equals(zcList.get(j).getMingcheng())){
                            Element zkElement=zcElement.addElement("钻孔");
                            zkElement.addAttribute("mingcheng", zkList.get(k).getMingcheng());
                            zkElement.addAttribute("fangweijiao", zkList.get(k).getFangwei());
                            zkElement.addAttribute("qingjiao", zkList.get(k).getQingjiao());
                            zkElement.addAttribute("chengkong", zkList.get(k).getChengkong());
                            zkElement.addAttribute("zhujiangqian", zkList.get(k).getZhujiangqian());
                            zkElement.addAttribute("shuiya", zkList.get(k).getShuiya());
                            zkElement.addAttribute("kongshen", zkList.get(k).getKongshen());
                            zkElement.addAttribute("va", zkList.get(k).getVa());
                            zkElement.addAttribute("vb", zkList.get(k).getVb());
                            zkElement.addAttribute("vc", zkList.get(k).getVc());
                            zkElement.addAttribute("niantu", zkList.get(k).getNiantu());
                            zkElement.addAttribute("shuini", zkList.get(k).getShuini());
                            zkElement.addAttribute("zhongya", zkList.get(k).getZhongya());
                            zkElement.addAttribute("beizhu", zkList.get(k).getBeizhu());
                            zkElement.addAttribute("zuanchang", zkList.get(k).getZuanchang());
                            zkElement.addAttribute("csdjl", zkList.get(k).getChushuidianshuipingtouying());
                        }
                    }
                }
            }
        }
    }
    public String getFilepath() {
        return filepath;
    }

    public void setFilepath(String filepath) {
        this.filepath = filepath;
    }
}
